<?php
namespace app\common\authtoken;


/**
 * 生成token
 * @package app\common\authtoken
 */
class token{
    /**
     * 产生随机token
     */
    public function randToken()
    {
        $request = \think\Request::instance();
        $api_token_server = md5(strtolower($request->module()) . strtolower($request->controller()) . strtolower($request->action()) . date('Y-m-d H:i:s', time()) . config('wx_secret.client_secret'));
        return $api_token_server;
    }

    /**
     * token 有效期一天
     */
    public function getToken($client_secret)
    {
        $request = \think\Request::instance();
        $api_token_server = md5(self::uncamelize($request->module()) . self::uncamelize($request->controller()) . strtolower($request->action()) . date('Y-m-d', time()) . $client_secret);
        return $api_token_server;
    }

    /**
     * @param $client_secret
     * @param string $route 请求的路由如：'api/album/index'
     * @return string
     */
    public static function getSignForTest($client_secret, $route) {
        $arr = explode('/', $route);
        return md5($arr[0] . $arr[1] . $arr[2] . date('Y-m-d', time()) . $client_secret);
    }

    /**
    * 驼峰命名转下划线命名
    * 思路:
    * 小写和大写紧挨一起的地方,加上分隔符,然后全部转小写
    */
    public static function uncamelize($camelCaps,$separator='_')
    {
     return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
    }
}
?>